package org.csu.mypetstore.service;
import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.CartDAO;
import org.csu.mypetstore.persistence.CategoryDAO;
import org.csu.mypetstore.persistence.Impl.CartDAOImpl;
import org.csu.mypetstore.persistence.Impl.CategoryDAOImpl;
import org.csu.mypetstore.persistence.Impl.ItemDAOImpl;
import org.csu.mypetstore.persistence.Impl.ProductDAOImpl;
import org.csu.mypetstore.persistence.ItemDAO;
import org.csu.mypetstore.persistence.ProductDAO;

import java.util.List;
//商品显示相关
public class CatalogService
{
    private CategoryDAO categoryDAO;
    private ProductDAO productDAO;
    private ItemDAO itemDAO;
    private CartDAO cartDAO;

    public CatalogService(){
        categoryDAO = new CategoryDAOImpl();
        productDAO = new ProductDAOImpl();
        itemDAO = new ItemDAOImpl();
        cartDAO = new CartDAOImpl();
    }
    //获得类的list
    public List<Category> getCategoryList() {
        return categoryDAO.getCategoryList();
    }
    //根据类的id获得类
    public Category getCategory(String categoryId) {
        return categoryDAO.getCategory(categoryId);
    }
    //根据产品的id获得产品
    public Product getProduct(String productId) {
        return productDAO.getProduct(productId);
    }
    //根据类的id获得产品list
    public List<Product> getProductListByCategory(String categoryId) {
        return productDAO.getProductListByCategory(categoryId);
    }

    //查找产品中有关键词keyword的产品list
    public List<Product> searchProductList(String keyword) {
        return productDAO.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    //根据产品id获得商品list
    public List<Item> getItemListByProduct(String productId) {
        return itemDAO.getItemListByProduct(productId);
    }

    //根据商品id获得商品
    public Item getItem(String itemId) {
        return itemDAO.getItem(itemId);
    }

    //某个商品是否有内存
    public boolean isItemInStock(String itemId) {
        return itemDAO.getInventoryQuantity(itemId) > 0;
    }

    //根据用户id找购物车
    public Cart getCartByUsername(String username){
        return cartDAO.getCartByUsername(username);
    }

    //根据用户删除购物车
    public boolean deleteCartByUsername(String username){
        return cartDAO.deleteCartByUsername(username);
    }

    //插入购物侧
    public boolean insertCartItem(CartItem cartItem, String username){
        return cartDAO.insertCart(cartItem,username);
    }

    //更新购物车
    public boolean updateCartItem(String username ,String itemId,int quantity){
        return cartDAO.updateCartItem(username ,itemId,quantity);
    }


    //删除购物车
    public boolean removeCartItem(String username ,String itemId){
        return cartDAO.removeCartItem(username,itemId);
    }

}
